package com.vincent.controller.admin;

import com.vincent.common.R;
import com.vincent.entity.Menu;
import com.vincent.entity.User;
import com.vincent.form.MenuForm;
import com.vincent.service.IMenuService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@RestController
@RequestMapping("/admin/user")
public class UserController {
    @Autowired
    private IMenuService menuService;

    @GetMapping("/menuList")
    public R menuList(){
        List<Menu> menus = null;
        User user = (User)SecurityUtils.getSubject().getPrincipal();

        MenuForm form = new MenuForm();
        if(user.getRole().getType() == 1){
            form.setRoleId(null);
            form.setCondition("m.enabled=1");
            menus = menuService.menusByForm(form);
        }
        else{
            form.setRoleId(null);
            form.setCondition("m.enabled=1");
            menus = menuService.menusByForm(form);
        }
        Set<String> perms = new HashSet<>();
        if(menus != null){
            for(Menu menu : menus) {
                perms.add(menu.getPerms());
                if (menu.getSubMenus() != null) {
                    for (Menu subMenu : menu.getSubMenus()) {
                        perms.add(subMenu.getPerms());
                    }
                }
            }
        }
        return R.ok().put("menuList",menus).put("perms",perms);
    }

    @GetMapping("/allMenuList")
    public R allMenuList(){
        MenuForm form = new MenuForm();
        form.setRoleId(null);
        List<Menu> menus = menuService.menusByForm(form);
        return R.ok().put("menuList",menus);
    }
}
